package com.mobile.web.config.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.mobile.util.RedisUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import com.mobile.util.Const;
import com.mobile.util.PageData;
import redis.clients.jedis.Jedis;

/**
 * @ClassName : Jurisdiction.java
 * @Decription : 权限处理
 * @Version : 2018/01/11  hlh  新建作成   1.0
 */
public class Jurisdiction {

	/**获取当前功能页面按钮权限
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getHC(String permis){
		Jedis jedis = new Jedis();

		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<String> userAuthc = new ArrayList<String>();
		//redis获取数据
		userAuthc =  (List<String>)getSession().getAttribute(getUserCode() + Const.SESSION_menuList);
		String arr[];
		String authArr[];
		for (int i = 0; i < userAuthc.size(); i++) {
			authArr = userAuthc.get(i).split(":");
			if(authArr[0].equals(permis)) {
				arr = userAuthc.get(i).split(":");
				if(arr.length > 2) {
					if (Integer.valueOf(arr[2]) == 1){
						resultMap.put(arr[1], true);
					}else {
						resultMap.put(arr[1], false);
					}
				}
			}
		}
		return resultMap;
	}
	/**获取当前功能页面按钮权限
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getHC(String userCode,String permis){
		Jedis jedis = RedisUtil.getJedis();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<String> userAuthc = new ArrayList<String>();
		//redis获取数据 (List<String>)
		userAuthc = JSONArray.parseArray(jedis.hget(Const.SESSION_menuList,userCode), String.class);
		String arr[];
		String authArr[];
		for (int i = 0; i < userAuthc.size(); i++) {
			authArr = userAuthc.get(i).split(":");
			if(authArr[0].equals(permis)) {
				arr = userAuthc.get(i).split(":");
				if(arr.length > 2) {
					if (Integer.valueOf(arr[2]) == 1){
						resultMap.put(arr[1], true);
					}else {
						resultMap.put(arr[1], false);
					}
				}
			}
		}
		jedis.close();
		return resultMap;
	}
	/**获取当前功能页面按钮权限
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, Object> getHCs(String userCode,List<String> permis){
		Jedis jedis = RedisUtil.getJedis();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<String> userAuthc = new ArrayList<String>();
		//redis获取数据 (List<String>)
		userAuthc = JSONArray.parseArray(jedis.hget(Const.SESSION_menuList,userCode), String.class);
		String arr[];
		String authArr[];
		for (int i = 0; i < userAuthc.size(); i++) {
			authArr = userAuthc.get(i).split(":");
			if(permis.contains(authArr[0])) {
				arr = userAuthc.get(i).split(":");
				if(arr.length > 2) {
					if (Integer.valueOf(arr[2]) == 1){
						resultMap.put(arr[0]+"-"+arr[1], true);
					}else {
						resultMap.put(arr[0]+"-"+arr[1], false);
					}
				}
			}
		}
		jedis.close();
		return resultMap;
	}
	/**获取当前登录账号
	 * @return
	 */
	public static String getUserCode(){
		return getSession().getAttribute(Const.SESSION_USERCODE).toString();
	}

	/**获取当前登录的用户
	 * @return
	 */
	public static PageData getUserInfo(){
		return (PageData)getSession().getAttribute(Const.SESSION_USER);
	}

	/**shiro管理的session
	 *
	 */
	public static Session getSession(){
		return SecurityUtils.getSubject().getSession();
	}
}
